package com.neusoft.a312cxy.android18cxy.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.a312cxy.android18cxy.Listener.ClubListListener;
import com.neusoft.a312cxy.android18cxy.R;
import com.neusoft.a312cxy.android18cxy.adapter.clubAdapter;
import com.neusoft.a312cxy.android18cxy.bean.ClubListBean;
import com.neusoft.a312cxy.android18cxy.model.ClubModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Young on 2017/9/25.
 */

public class ClubFragment extends Fragment implements ClubListListener,View.OnClickListener{
    private RecyclerView recyclerView;
    List<ClubListBean> list=new ArrayList<>();
    private EditText searchContent;
    private ImageView searchBtn;
    private clubAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view=inflater.inflate(R.layout.fragment_club, container, false);
        initRecyclerView(view);
        ClubModel model=new ClubModel();
        model.getList("45",1,this);
        adapter=new clubAdapter(getActivity(),list);
        recyclerView.setAdapter(adapter);
        return  view;
    }

    public  void initRecyclerView(View view){

        recyclerView= (RecyclerView) view.findViewById(R.id.ac);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        searchContent=(EditText)view.findViewById(R.id.search_edit);
        searchBtn=(ImageView)view.findViewById(R.id.search_button1);
        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search();
            }
        });
    }

    @Override
    public void onResponse(List<ClubListBean> listBeen) {
        list=listBeen;
       adapter.setList(list);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(getActivity(), ""+msg, Toast.LENGTH_SHORT).show();
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.search_button1:
                search();
                break;
        }

    }

    private void search() {
        List<ClubListBean> tmp=new ArrayList<>();
        String content=searchContent.getText().toString();
        for (int i = 0; i <list.size() ; i++) {
            if(list.get(i).getClub_name().contains(content)||list.get(i).getCity_name().contains(content)){
                tmp.add(list.get(i));
            }/*else {
                ClubModel model=new ClubModel();
                model.getList("45",1,this);
                adapter=new clubAdapter(getActivity(),list);

            }*/
        }
        adapter.setList(tmp);
        list=tmp;
    }

}